package com.bbex.mapper;

import com.bbex.entity.HongmengCoinHidVolume;
import com.bbex.sql.build.HongmengCoinHidVolumeSqlBuild;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

/**
 * 用户hid资产的操作信息；
 *
 * @author p
 */
@Mapper
public interface HongmengCoinHidVolumeDao {

    @InsertProvider(type = HongmengCoinHidVolumeSqlBuild.class, method = "insert")
    void insert(HongmengCoinHidVolume hongmengCoinVolume);

    @SelectProvider(type = HongmengCoinHidVolumeSqlBuild.class, method = "findById")
    HongmengCoinHidVolume findById(String id);

    @UpdateProvider(type = HongmengCoinHidVolumeSqlBuild.class, method = "updateById")
    long updateById(HongmengCoinHidVolume hongmengCoinVolume);

    @Select("select id,user_id as userId,coin_id as coinId,version,coin_symbol as coinSymbol,volume,lock_volume as lockVolume,out_lock_volume as outLockVolume," +
            "flag,flag_mark as flagMark,create_by as createBy,update_by as updateBy,create_date as createDate,update_date as updateDate " +
            "from hongmeng_user_coin_hid_volume where user_id = #{userId}")
    List<HongmengCoinHidVolume> findHidAll(String userId);

    @Select("select id,user_id as userId,coin_id as coinId,version,coin_symbol as coinSymbol,volume,lock_volume as lockVolume,out_lock_volume as outLockVolume," +
            "flag,flag_mark as flagMark,create_by as createBy,update_by as updateBy,create_date as createDate,update_date as updateDate " +
            "from hongmeng_user_coin_hid_volume where user_id = #{userId} and coin_id = #{coinId} limit 1")
    HongmengCoinHidVolume findByUserIdAndCoinId(@Param("userId") String userId, @Param("coinId") String coinId);

    @Update("update hongmeng_user_coin_hid_volume set volume = #{volume},version= version+1 where user_id = #{userId} and coin_id = #{coinId} and version= #{version}")
    long updateVolume(@Param("userId") String userId, @Param("coinId") String coinId, @Param("volume") BigDecimal addVolume, @Param("version") Long version);

    //Long updateHidVolume(@Param("userId") String userId, @Param("coinId") String coinId, @Param("volume") BigDecimal addVolume, @Param("updateDate") Timestamp updateDate, @Param("version") Long version);
    @Update("update hongmeng_user_coin_hid_volume set volume = #{volume},version= version+1 where user_id = #{userId} and coin_id = #{coinId} and update_date = #{updateDate} and version= #{version}")
    long updateHidVolume(@Param("userId") String userId, @Param("coinId") String coinId, @Param("volume") BigDecimal addVolume, @Param("updateDate") Timestamp updateDate, @Param("version") Long version);

    @Update("update hongmeng_user_coin_hid_volume set volume = volume + #{addVolume},version = version + 1 where user_id = #{userId} and coin_id = #{coinId} and volume >= 0")
    long updateAddVolume(@Param("userId") String userId, @Param("coinId") String coinId, @Param("addVolume") BigDecimal addVolume);

}
